<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/**
 * Class ManyiController
 * @package Pay\Controller
 * 乐享
 */
class LexiangController extends PayController{
    protected $config;

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>'054']);
        }

        $product = Product::getInstance()->getById(I("request.pay_bankcode",0,"intval"));

        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>'054']);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );

        $data = array();
        $data['fxid'] = $this->config['mch_id'];
        $data['fxddh'] = $return['orderid'];
        $data['fxdesc'] = 'zf';
        $data['fxfee'] = $return['amount'];
        $data['fxnotifyurl'] = $return['notifyurl'];
        $data['fxbackurl'] = $return['callbackurl'];

        $ismobile = isMobile();

        switch($this->config['paytype']){
            //微信
            case 1:
                $this->showmessage('暂不支持此方式!',['code'=>'054']);
                break;
            //微信H5
            case 2:
                $this->showmessage('暂不支持此方式!',['code'=>'054']);
                break;
            //支付宝扫码
            case 3:
                $data['fxpay'] = 'alipaywap';
                break;
            //支付宝H5
            case 4:
                $data['fxpay'] = 'alipaywap';
                break;
            //网银直连
            case 6:
                $this->showmessage('暂不支持此方式!',['code'=>'054']);
                break;
            default:
                $this->showmessage('暂不支持此方式!',['code'=>'054']);

        }
        $data['fxattch'] = 'zf';
        $data['fxip'] = $_SERVER['REMOTE_ADDR'];

        $str = "fxid=".$data["fxid"] . "&fxddh=".$data["fxddh"] . "&fxfee=".$data["fxfee"] . "&fxnotifyurl=".$data["fxnotifyurl"] . "&" . $this->config['signkey'];
        $data["fxsign"] = md5($str);
        $res = curl($this->config['gateway'], arrToUrlStr($data));

        if(!$res){
            $this->showmessage('未获取到数据!',['code'=>'054']);
        }
        $res = json_decode($res, true);
        if(empty($res)){
            $this->showmessage('获取的数据类型错误!',['code'=>'054']);
        }

        /*echo '<pre/>';
        print_r($data);
        echo "请求地址： " . $this->config['gateway'] . "<br/>";
        echo "签名字符串： " . $str ."<br/>";
        print_r($res); exit; */

        if(!$res['payurl']){
            if($res['error']){
                $this->showmessage($res['error'],['code'=>'019']);
            }
            $this->showmessage("获取支付链接失败",['code'=>'019']);
        }

        header("location: {$res['payurl']}");
    }

    //通知
    public function notifyurl(){
        //file_put_contents("a.txt", var_export($_POST,true)."\r\n\r\n");
        //file_put_contents("a.txt", var_export($_GET,true)."\r\n\r\n", FILE_APPEND);
        //file_put_contents("a.txt", file_get_contents('php://input'), FILE_APPEND);
        //file_put_contents("a.txt",  date("Y-m-d H:i:s",time())."\r\n\r\n", FILE_APPEND);

        $p = I("post.");
        /*$p = array (
          'fxid' => '2018134',
          'fxddh' => '201810312004495062789',
          'fxorder' => '2018103122001455945418679104',
          'fxdesc' => 'zf',
          'fxfee' => '10.00',
          'fxattch' => 'zf',
          'fxstatus' => '1',
          'fxtime' => '1540987651',
          'fxsign' => 'b1fd0989d50f64089ded49dc17e468e7',
        );*/

        if(empty($p)){
            exit("");
        }

        //获取订单信息
        $orderid = $p['fxddh'];
        $find = $this->checkOrderNotify($orderid);
        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(

            "pay_orderid_rd"=> htmlspecialchars($p['fxorder'])
        ));

        //获取渠道信息
        $this->config = Channel::getInstance()->find(['code'=>'Lexiang']);

        if($p['fxstatus'] != 1){
            exit("fail");
        }

        $sign = $p['fxsign'];

        //md5(订单状态+商务号+商户订单号+支付金额+商户秘钥
        $str = "fxstatus=" . $p['fxstatus']."&fxid=" . $p['fxid'] . "&fxddh=" . $p['fxddh'] . "&fxfee=" . $p['fxfee'] . "&".$this->config['signkey'];
        $sign2 = md5($str);

        if($sign != $sign2){
            echo 'sign_wrong';exit;
        }

        //处理支付
        $this->EditMoney($find['pay_orderid'],"Lexiang",0);

        //回调成功
        echo 'success'; exit;
    }

    public function callBackUrl(){

        echo '操作成功'; exit;
    }
}